<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Services\OrganizeService;
use App\Http\Requests\OrganizeRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class OrganizeController extends ApiController
{
    public function index($pid = 0)
    {
        $organizes = OrganizeService::index($pid);
        return $this->apiReturn(0, 'success', $organizes);
    }

    public function store(Request $request)
    {
        try {
            $data = [];
            $data['pid'] = $request->post('pid');
            $data['organize_name'] = $request->post('organize_name');
            $data['path'] = OrganizeService::getOrganizePath($data['pid']);
            OrganizeService::store($data);
            return $this->apiReturn(0, 'success');
        } catch (\Exception $exception) {
            return $this->apiReturn(1, $exception->getMessage());
        }
    }

    public function show($id)
    {
        try {
            $organize = OrganizeService::findOne($id);
            return $this->apiReturn(0, 'success', $organize);
        } catch (\Exception $exception) {
            return $this->apiReturn(1, $exception->getMessage());
        }
    }

    public function update(Request $request, $id)
    {
       try{
           $data = [];
           $data['organize_name'] = $request->input('organize_name');
           $data['pid'] = $request->input('pid');
           OrganizeService::update($id,$data);
           return $this->apiReturn(0, '保存成功');
       }catch (\Exception $exception){
           return $this->apiReturn(1, $exception->getMessage());
       }
    }
}
